﻿using JsonConversion;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using RTracker.Models.Common;
using RTracker.Models.ErrorLog;
using System.Web.Security;

namespace RTracker.Areas.Recruitment.Models.CandidateDriveStatus
{
    public class MCandidateDriveStatus
    {
        /// static variable RowSize  from config settings to get list of this row size.
        public static int nRowSize = Convert.ToInt32(ConfigurationManager.AppSettings["TotalRecordsInListUser"]);

        /// <summary>
        /// This method is used to create a CandidateDriveStatus in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateDriveStatus">This object contains details of the CandidateDriveStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details</returns>
        public CStatus CreateCandidateDriveStatus(CAuthenticationContext objCAuthenticationContext, ref CCandidateDriveStatus objCCandidateDriveStatus)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_create_candidate_drive_status_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@candidate_drive_status_name", SqlDbType.NVarChar).Value = objCCandidateDriveStatus.CandidateDriveStatusName;
                    cmd.Parameters.Add("@candidate_drive_status_desc", SqlDbType.NVarChar).Value = objCCandidateDriveStatus.CandidateDriveStatusDesc;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCCandidateDriveStatus.CreatedBy;  
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// This method is used to delete a CandidateDriveStatus in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateDriveStatus">This object contains details of the CandidateDriveStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus DeleteCandidateDriveStatus(CAuthenticationContext objCAuthenticationContext, ref CCandidateDriveStatus objCCandidateDriveStatus)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_delete_candidate_drive_status_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCCandidateDriveStatus.CreatedBy;
                    cmd.Parameters.Add("@candidate_drive_status_ids", SqlDbType.NVarChar).Value = objCCandidateDriveStatus.CandidateDriveStatusIds;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// This method is used to update the CandidateDriveStatus detail in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateDriveStatus">This object contains details of the CandidateDriveStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus UpdateCandidateDriveStatus(CAuthenticationContext objCAuthenticationContext, ref CCandidateDriveStatus objCCandidateDriveStatus)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_update_candidate_drive_status_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@candidate_drive_status_id", SqlDbType.Int).Value = objCCandidateDriveStatus.CandidateDriveStatusId;
                    cmd.Parameters.Add("@candidate_drive_status_name", SqlDbType.NVarChar).Value = objCCandidateDriveStatus.CandidateDriveStatusName;
                    cmd.Parameters.Add("@candidate_drive_status_desc", SqlDbType.NVarChar).Value = objCCandidateDriveStatus.CandidateDriveStatusDesc;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// This method is used to get the CandidateDriveStatus detail in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateDriveStatus">This object contains details of the CandidateDriveStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus GetCandidateDriveStatusDetail(CAuthenticationContext objCAuthenticationContext, ref CCandidateDriveStatus objCCandidateDriveStatus)
        {
            CStatus oCStatus = new CStatus();
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("r_get_candidate_drive_status_detail_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@candidate_drive_status_id", SqlDbType.BigInt).Value = objCCandidateDriveStatus.CandidateDriveStatusId;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;

                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    if (rowsAffected > 0)
                    {
                        objCCandidateDriveStatus = dt.ConvertToListFromDataTableCandidateDriveStatus()[0];
                    }

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.DetailSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Sets the softlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateDriveStatus">This object contains details of the CandidateDriveStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus SetCandidateDriveStatusSoftLock(CAuthenticationContext objCAuthenticationContext, CCandidateDriveStatus objCCandidateDriveStatus)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_set_candidate_drive_status_soft_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@candidate_drive_status_id", SqlDbType.BigInt).Value = objCCandidateDriveStatus.CandidateDriveStatusId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Sets the hardlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateDriveStatus">This object contains details of the CandidateDriveStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus SetCandidateDriveStatusHardLock(CAuthenticationContext objCAuthenticationContext, CCandidateDriveStatus objCCandidateDriveStatus)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_set_candidate_drive_status_hard_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@candidate_drive_status_id", SqlDbType.BigInt).Value = objCCandidateDriveStatus.CandidateDriveStatusId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Releases the softlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateDriveStatus">This object contains details of the CandidateDriveStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus ReleaseCandidateDriveStatusSoftLock(CAuthenticationContext objCAuthenticationContext, CCandidateDriveStatus objCCandidateDriveStatus)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_release_candidate_drive_status_soft_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@candidate_drive_status_id", SqlDbType.Int).Value = objCCandidateDriveStatus.CandidateDriveStatusId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Releases the hardlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateDriveStatus">This object contains details of the CandidateDriveStatus.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus ReleaseCandidateDriveStatusHardLock(CAuthenticationContext objCAuthenticationContext, CCandidateDriveStatus objCCandidateDriveStatus)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_release_candidate_drive_status_hard_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@candidate_drive_status_id", SqlDbType.Int).Value = objCCandidateDriveStatus.CandidateDriveStatusId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCCandidateDriveStatus.CreatedBy;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        ///  This function is used to get complete CandidateDriveStatus list
        /// </summary>
        /// <returns>List of type CCandidateDriveStatus</returns>
        public GCandidateDriveStatus GetCandidateDriveStatusGridList(CCandidateDriveStatus objCCandidateDriveStatus, CAuthenticationContext objCAuthenticationContext)
        {
            GCandidateDriveStatus objGCandidateDriveStatus = new GCandidateDriveStatus();
            int nTotalRecords = 0;
            int nCurrentRowSize = nRowSize;

            if (objCCandidateDriveStatus.RowSize != 0)
            {
                nCurrentRowSize = objCCandidateDriveStatus.RowSize;
            }
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("r_get_candidate_drive_status_paging_list_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@candidate_drive_status_id", SqlDbType.Int).Value = objCCandidateDriveStatus.CandidateDriveStatusId;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCCandidateDriveStatus.OrganizationId;
                    cmd.Parameters.Add("@page_number", SqlDbType.Int).Value = objCCandidateDriveStatus.PageNumber;
                    cmd.Parameters.Add("@row_size", SqlDbType.Int).Value = nCurrentRowSize;
                    cmd.Parameters.Add("@sort_on", SqlDbType.VarChar).Value = objCCandidateDriveStatus.SortOn;
                    cmd.Parameters.Add("@sort_type", SqlDbType.VarChar).Value = objCCandidateDriveStatus.SortType;
                    cmd.Parameters.Add("@search", SqlDbType.VarChar).Value = objCCandidateDriveStatus.Search;
                    cmd.Parameters.Add("@searchon", SqlDbType.VarChar).Value = objCCandidateDriveStatus.SearchOn;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    objGCandidateDriveStatus.Paging = new CPaging();
                    if (rowsAffected > 0)
                    {
                        objGCandidateDriveStatus.LstCandidateDriveStatus = dt.ConvertToListFromDataTableCandidateDriveStatus();
                        objGCandidateDriveStatus.LstCandidateDriveStatus[0].PageNumber = int.Parse(cmd.Parameters["@idOut"].Value.ToString());
                        objGCandidateDriveStatus.Paging.PageNumber = objGCandidateDriveStatus.LstCandidateDriveStatus[0].PageNumber;
                    }
                    else
                    {
                        objGCandidateDriveStatus.LstCandidateDriveStatus = null;
                    }


                    nTotalRecords = int.Parse(cmd.Parameters["@error"].Value.ToString());

                    objGCandidateDriveStatus.Status = new CStatus();

                    if (nTotalRecords < 0)
                    {
                        objGCandidateDriveStatus.Status.Success = false;
                        objGCandidateDriveStatus.Status.StatusDetails = nTotalRecords.DbError();
                    }
                    else
                    {
                        objGCandidateDriveStatus.Paging.TotalRecordsCount = nTotalRecords;
                        objGCandidateDriveStatus.Paging.PageSize = nCurrentRowSize;

                        objGCandidateDriveStatus.Status.Success = true;
                        objGCandidateDriveStatus.Status.StatusDetails = "success";
                    }
                }
                catch (Exception ex)
                {
                    objGCandidateDriveStatus.Status = new CStatus();
                    objGCandidateDriveStatus.Status.Success = false;
                    objGCandidateDriveStatus.Status.StatusDetails = ex.Message;
                }
            }
            return objGCandidateDriveStatus;
        }
    }
}